package com.aaa.web.shiro;


import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yyh
 * @Date: 2018/12/12 0012 21:41
 * @Version 1.0
 */
@Configuration
public class ShiroConfiguration {
    //设置加密方式和加密次数
    @Bean
    public HashedCredentialsMatcher HashedCredentialsMatcherBean(){
        HashedCredentialsMatcher credentialsMatcher =new HashedCredentialsMatcher();
        credentialsMatcher.setHashIterations(1);
        credentialsMatcher.setHashAlgorithmName("MD5");
        return credentialsMatcher;
    }
    //将自己的验证方式加入容器
    @Bean
    public MyShiroRealm myShiroRealm(HashedCredentialsMatcher credentialsMatcher) {
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        myShiroRealm.setCredentialsMatcher(credentialsMatcher);
        return myShiroRealm;
    }

    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

    //权限管理，配置主要是Realm的管理认证
    @Bean
    public SecurityManager securityManager(MyShiroRealm  myShiroRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm);
        return securityManager;
    }

    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String,String> map = new HashMap<String, String>();
        map.put("/weblogin/*","anon");
        map.put("/accountSafe/*","anon");
        map.put("/appUserWallet/*","anon");
        map.put("/authentication/*","anon");
        map.put("/IDCardUploadImg/*","anon");
        map.put("/expenseCalendar/*","anon");
        map.put("/carInformation/*","anon");
        map.put("/login/*","anon");
        map.put("/recycle/*","anon");
        map.put("/notice/*","anon");
        map.put("/UploadHeadImgAndUserName/*","anon");
        map.put("/position/*","anon");
        map.put("/register/*","anon");
        map.put("/resetPsw/*","anon");
        map.put("/service/*","anon");
        map.put("/store/*","anon");
        map.put("/views/webhtml/login/login.html","anon");
        map.put("/layuiadmin/*","anon");
        map.put("/layuiadmin/*/*","anon");
        map.put("/layuiadmin/*/*/*","anon");
        map.put("/layuiadmin/*/*/*/*","anon");
        map.put("/layuiadmin/layui/lay/modules/*","anon");
        map.put("/layuiadmin/css/*","anon");
        map.put("/layuiadmin/config.js","anon");
        map.put("/layuiadmin/layui/css/layui.css","anon");
        map.put("/layuiadmin/layui/lay/modules/*.js","anon");
        map.put("/layuiadmin/layui/lay/modules/laytpl.js","anon");
        map.put("/layuiadmin/layui/lay/modules/layer.js","anon");
        map.put("/logout","logout");
        //对所有用户认证
        //map.put("/**","authc");
        //登录
        shiroFilterFactoryBean.setLoginUrl("/weblogin/goLogin");
        //首页
        shiroFilterFactoryBean.setSuccessUrl("/per/getPer");
        //错误页面，认证不通过跳转
        shiroFilterFactoryBean.setUnauthorizedUrl("/error");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    //加入注解的使用，不加入这个注解不生效
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}